package com.ocm.commerceinteraction.ui;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTabHost;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TabHost;
import android.widget.Toast;

import com.ocm.commerceinteraction.R;
import com.ocm.commerceinteraction.util.helper.LogHelper;
import com.ocm.commerceinteraction.util.loading.Loading;
import com.ocm.commerceinteraction.util.player.Player;
import com.readystatesoftware.systembartint.SystemBarTintManager;

import easeui.EaseConstant;

/**
 * Created by Curry on 2016-4-28.
 */
public class ChatActivity extends BaseFragmentActivity {
    private static ChatActivity chatActivity;
    private static FragmentManager fragmentManager;
    public LayoutInflater layoutInflater;
    private String group_uniq_id;
    private Player player;
    private int from  = 1;

    //定义数组来存放按钮图片
    private int mImageViewArray[] = {R.drawable.tab_1_btn, R.drawable.tab_2_btn, R.drawable.tab_3_btn,
            R.drawable.tab_4_btn};


    public static ChatActivity getInstance() {
        return chatActivity;
    }

    public static FragmentManager getFragmentMangerinstance() {
        return fragmentManager;
    }

    //    Fragment chatFragment = new ChatFragment_();
    SeekBar music_progress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);


        // 透明状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = getWindow();
            // Translucent status bar
            window.setFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
//        //透明状态栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        setTranslucentStatus();
        chatActivity = this;
        Loading.mContext = this;
        getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN); // 解决键盘顶tab问题
        fragmentManager = getSupportFragmentManager();
        layoutInflater = LayoutInflater.from(this);
      //  music_progress = (SeekBar) findViewById(R.id.music_progress);
//        player = new Player(music_progress);
        from = 1; //1表示个人聊天，2表示群聊
        if (getIntent().getExtras().getInt(EaseConstant.EXTRA_CHAT_TYPE) == EaseConstant.CHATTYPE_GROUP){
            from = 2;
//            new Thread(new Runnable() {
//                @Override
//                public void run() {
////                player.playUrl("http://abv.cn/music/光辉岁月.mp3");
//                 // player.playUrl("http://bchat.ocmcom.com/data/upload/vidio/djhot.mp3");
//                }
//            }).start();
        }
        Bundle bundle = new Bundle();
        bundle.putInt(EaseConstant.EXTRA_CHAT_TYPE, getIntent().getExtras().getInt(EaseConstant.EXTRA_CHAT_TYPE));
        bundle.putString(EaseConstant.EXTRA_USER_ID, getIntent().getExtras().getString(EaseConstant.EXTRA_USER_ID));
        bundle.putString("groupUniqId", getIntent().getExtras().getString("groupUniqId"));
        bundle.putString("name", getIntent().getExtras().getString("name"));
        bundle.putString("notice", getIntent().getExtras().getString("notice"));
        bundle.putString("is_block", getIntent().getExtras().getString("is_block"));
        bundle.putInt("from", from);
        Fragment chatFragment = new ChatFragment_();
        chatFragment.setArguments(bundle);
        getSupportFragmentManager().beginTransaction().add(R.id.content_chat, chatFragment).commit();



    }


    private boolean is2CallBack = false;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (fragmentManager.getBackStackEntryCount() != 0) {
                fragmentManager.popBackStack();
            } else {
                Intent intent = new Intent();
                intent.putExtra("dataChange", dataChange);
                setResult(002, intent);
                finish();
            }
        }else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN){
            ChatFragment.onKeyDown(keyCode,event);
        } else  if (keyCode == KeyEvent.KEYCODE_VOLUME_UP){
            ChatFragment.onKeyDown(keyCode,event);
        }



        return true;
    }

    /**
     * @param from       当前fragment
     * @param toFragment 跳转到的fragment
     */
    public static void switchFragment(Fragment from, Fragment toFragment) {
        if (toFragment.isAdded())
            return;
        getInstance().getSupportFragmentManager()
                .beginTransaction()
                .hide(from).replace(R.id.content_none_bottom, toFragment).addToBackStack(null).commit();
    }

    @Override
    protected void onResume() {
        super.onResume();
        dataChange = false;
//        if (chatFragment != null){
//            chatFragment.onResume();
//        }
        //注：回调 1
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (player != null) {
            player.pause();
        }
        //注：回调 2
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        //注：回调 3
        return super.dispatchTouchEvent(event);
    }

    @Override
    void initView() {

    }

    @Override
    void Click(View view) {

    }

    public static boolean dataChange = false;

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (player != null) {
            player.stop();
        }
    }
}
